package com.xiangmo.travel.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangmo.travel.Annotation.TokenRequired;
import com.xiangmo.travel.Dto.AttractionsDto;
import com.xiangmo.travel.Dto.NoticeDto;
import com.xiangmo.travel.Dto.PageInfoDto;
import com.xiangmo.travel.Dto.ResultData;
import com.xiangmo.travel.Dvo.AttractionsDvo;
import com.xiangmo.travel.Entity.Attractions;
import com.xiangmo.travel.Enums.CodeAndMsg;
import com.xiangmo.travel.Service.AttractionsService;
import com.xiangmo.travel.Service.NoticeService;
import com.xiangmo.travel.Utils.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 景点模块
 * 包括：发布景点
 */
@Slf4j
@RestController
@RequestMapping("/api/attractions")
public class AttractionsController {
    @Autowired
    private AttractionsService attractionsService;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private NoticeService noticeService;

    /**
     * 发布景点
     * @param attractionsDto
     * @param token
     * @return
     */
    @TokenRequired
    @PostMapping("/published")
    public ResultData<Attractions> publicAttractions(@RequestBody AttractionsDto attractionsDto,
                                                     @RequestHeader("Authorization") String token){
        attractionsDto.setUserId(attractionsDto.getUserId());
        Long status = attractionsService.publishAttractions(attractionsDto);
        NoticeDto notice = new NoticeDto();
        notice.setName(attractionsDto.getTitle());
        notice.setSendUserId(attractionsDto.getUserId());
        notice.setTeamId(status);
        noticeService.publishedAttractions(notice);
        //文章发布失败
        if(status == null){
            log.warn("景点发布失败!");
            return new ResultData<>(CodeAndMsg.ERROR.getCode(),
                    CodeAndMsg.ERROR.getMessage(), null, token.substring(7));
        }
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), null, token.substring(7));
    }

    //根据类别查询景点
    @GetMapping("/search/type/{type}")
    public ResultData<PageInfoDto<AttractionsDvo>> getAttractions(@RequestParam int pageNum, @RequestParam int pageSize,
                                                        @PathVariable String type){
        PageInfoDto<AttractionsDvo> attractionsList = attractionsService.attractionsPageByType(pageNum, pageSize, type);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), attractionsList, null);
    }


    /**
     * 根据分页查询景点
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public ResultData<PageInfoDto<AttractionsDvo>> getAttractions(@RequestParam int pageNum,@RequestParam int pageSize){
        PageInfoDto<AttractionsDvo> attractionsPage = attractionsService.attractionsPage(pageNum, pageSize);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), attractionsPage, null);
    }

    /**
     * 根据id获取文章详情页面
     * @param id
     * @return
     */
    @GetMapping("/articles/{id}")
    public ResultData<Attractions> getArticle(@PathVariable Long id) {
        Attractions attractions = attractionsService.getAttractionsById(id);
        if (attractions == null) {
            log.warn("根据id获取文章详情页面失败!");
            return new ResultData<>(CodeAndMsg.ATTRACTIONS_CONTENT_ERROR.getCode(),
                    CodeAndMsg.ATTRACTIONS_CONTENT_ERROR.getMessage(), null, null);
        } else {
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), attractions, null);
        }
    }

    //搜索景点 通用
//    @TokenRequired
    @GetMapping("/search/{content}")
    public ResultData<PageInfoDto<AttractionsDvo>> searchAttractions(@RequestParam int pageNum,@RequestParam int pageSize,
            @PathVariable String content){
        PageInfoDto<AttractionsDvo> attractionsPage = attractionsService.searchAttractions(pageNum, pageSize, content);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), attractionsPage, null);
    }

    //搜索景点 通过分类前提
    @GetMapping("/search/{type}/{content}")
    public ResultData<PageInfoDto<AttractionsDvo>> searchAttractionsByType(@RequestParam int pageNum,@RequestParam int pageSize,@PathVariable String type,
                                                           @PathVariable String content){
        PageInfoDto<AttractionsDvo> attractionsPage = attractionsService.searchAttractionsByType(pageNum, pageSize, content, type);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                CodeAndMsg.SUCCESS.getMessage(), attractionsPage, null);
    }

    //根据用户id查询发布的景点
    @TokenRequired
    @GetMapping("/user/{userId}")
    public ResultData<Page<Attractions>> getArticleByUserId(@PathVariable Long userId, @RequestParam int pageNum,  @RequestParam int pageSize,
                                                            @RequestHeader("Authorization") String token) {
        Page<Attractions> page = attractionsService.getAttractionsByUserId(userId, pageNum, pageSize);
        if (page == null) {
            return new ResultData<>(CodeAndMsg.ATTRACTIONS_CONTENT_ERROR.getCode(),
                    CodeAndMsg.ATTRACTIONS_CONTENT_ERROR.getMessage(), page, token.substring(7));
        } else {
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(),
                    CodeAndMsg.SUCCESS.getMessage(), page, token.substring(7));
        }
    }
}
